package com.orange.controller;


import com.github.pagehelper.PageInfo;
import com.orange.commons.Constant;
import com.orange.commons.Result;
import com.orange.domain.User;
import com.orange.domain.VisitLog;
import com.orange.dto.VisitLogDto;
import com.orange.service.VisitLogService;
import com.orange.utils.WebMvcUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author Emp
 * @projectName: crm
 * @package: com.powernode.controller
 * @className: VisitLogController
 * @description: TODO
 * @date: 2021/12/1 11:55
 * @version: 1.0
 */
@RestController
@RequestMapping("visitLog")
public class VisitLogController {

    @Resource
    private VisitLogService visitLogService;

    /**
     * 根据客户id和用户id分页查询拜访记录
     * @param visitLogDto
     * @return
     */
    @RequestMapping("visitLogList.do")
    public Result visitLogList(VisitLogDto visitLogDto){
        User user = WebMvcUtil.getCurrentUser();
        if (user.getRole().equals(Constant.USER_ROLE_2)) {
            visitLogDto.setUserId(user.getId());
        }
        PageInfo<VisitLog> visitLogInfo = visitLogService.queryByPage(visitLogDto);
        return new Result(0,"查询成功",visitLogInfo.getTotal(),visitLogInfo.getList());
    }

    /**
     * 新增拜访记录,VisitLog时间字段使用@DateTimeFormat修饰解决前端到后端时间格式转换问题 Err400
     * @param visitLog
     * @return
     */
    @RequestMapping("addVisitLog.do")
    public Result addVisitLog(VisitLog visitLog){
        try {
            User user = WebMvcUtil.getCurrentUser();
            visitLog.setUserId(user.getId());
            visitLog.setRealname(user.getRealname());
            visitLog.setCreateTime(new Date());
            int i = visitLogService.insert(visitLog);
            return i==1 ? Result.USER_ADD_SUCCESS : Result.USER_ADD_ERROR;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_ADD_ERROR;
        }
    }
}
